clear all 
set more off

* Decomposition of EUN.

* load
use "DecompositionResults_EUN_BWUN.dta", clear

* list of transitions
local xlist "EU EN UE NE EEnew_jj EEnew_occ"
tempfile dataio1
save `dataio1'

* re-arrange the dataset
local xcount = 1
foreach x of local xlist {

	use `dataio1', clear

	if "`x'" == "EU" | "`x'" == "EN" | "`x'" == "EEnew_jj" | "`x'" == "EEnew_occ" {
		local avar "g_easo"
	}
	else {
		local avar "g_eas"
	}

	*display in red "------------------ CODE `x'"
	reg aggtrans_`avar'_`x' lcgdp
	local slope = _b[lcgdp]

	reg country_`avar'_`x' lcgdp
	local ce = _b[lcgdp]

	reg comp_`avar'_`x' lcgdp
	local co = _b[lcgdp]

	clear
	set obs 3

	gen name = "slope" if _n == 1
	replace name = "composition" if _n == 2
	replace name = "country" if _n == 3

	gen count = _n

	gen `x' = .
	replace `x' = `slope' if name == "slope"
	replace `x' = `co'/(`co' + `ce' ) if name == "composition"
	replace `x' = `ce'/(`co' + `ce') if name == "country"

	capture rename EEnew_occ OO
	capture rename EEnew_jj JJ

	if `xcount' == 1 {
		tempfile dhold1
		save `dhold1'
	}
	else {
		merge 1:1 name using `dhold1'
		drop _merge

		tempfile dhold1
		save `dhold1'
	}	

	local xcount = `xcount' + 1
}



* turn it into a table
order count name EU EN UE NE JJ OO
sort count
drop count

label var EU "Exit to Unemp"
label var EN "Exit to Inactive"
label var UE "JFR from Unemp"
label var NE "JFR from Inactive"
label var JJ "Job-Job Rate"
label var OO "Occ Switching Rate"
label var name ""

tostring(EU EN UE NE JJ OO), replace force format("%15.2fc")

replace name = "Composition" if name == "composition"
replace name = "Country" if name == "country"
drop if name == "slope"
set obs 3

replace name = "Total" if name == ""
foreach x of varlist * {
	replace `x' = "1.000" if `x' == ""
}


* drop a blank for tex's sake
gen vb1 = .
order vb1, after(EN)


#delimit ;
texsave using "Figures/Table3.tex", 
title(Aggregate Flow Decomposition) 
varlabels
align(l c c c c)
nofix
footnote("\textit{Table notes:} Decomposition for aggregate flows, normalized by average across all country-years. Decomposition terms are given as percentages of that overall slope with log GDP per capita. Exit rates, Job-Job, and Occupational Switches decompose based on full set of interactions Age 
\$ \times \$ Education \$ \times \$ Sex \$ \times \$ Occupation. Job-finding rates uses Age 
\$ \times \$ Education \$ \times \$ Sex.") 
replace;
#delimit cr	

